package com.jiinfo.taglib.dic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.alibaba.fastjson.JSON;
import com.jiinfo.config.ConstConfig;
import com.jiinfo.model.DicNode;
import com.jiinfo.util.StringUtils;

public class GetSelectTag extends TagSupport{

	private static final long serialVersionUID = -1195747878774389237L;
	private static Map<String,List<DicNode>> map=new HashMap<String,List<DicNode>>();
	
	private String path;
	private String attributes;
	private Boolean isShowPlease;
	private String value;
	private String has;
	private String notHas;
	private String oneShow;
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getAttributes() {
		return attributes;
	}
	public void setAttributes(String attributes) {
		this.attributes = attributes;
	}
	public Boolean getIsShowPlease() {
		return isShowPlease;
	}
	public void setIsShowPlease(Boolean isShowPlease) {
		this.isShowPlease = isShowPlease;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getHas() {
		return has;
	}
	public void setHas(String has) {
		this.has = has;
	}
	public String getNotHas() {
		return notHas;
	}
	public void setNotHas(String notHas) {
		this.notHas = notHas;
	}
	public String getOneShow() {
		return oneShow;
	}
	public void setOneShow(String oneShow) {
		this.oneShow = oneShow;
	}
	@SuppressWarnings({ "unchecked", "rawtypes", "static-access" })
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();// 得到out
		try {
			if(path!=null&&!"".equals(path)){
				if(!map.containsKey(path)){
					String[] paths=path.split(ConstConfig.DIC_DIVISION);
					DicNode dicNode=DicNode.dao.findFirst("select * from t_dic_node t where t.parentId is null and t.code=?",paths[0]);
					for (int i=1;i<paths.length;i++) {
						List<DicNode> dicNodes=dicNode.getDicNodes();
						for (DicNode dicNode2 : dicNodes) {
							if(paths[i].equals(dicNode2.get("code"))){
								dicNode=dicNode2;
								break;
							}else{
								dicNode=null;
							}
						}
					}
					
					map.put(path, dicNode.getDicNodes());
				}
				
				List<DicNode> dicNodes=map.get(path);
				
				Map<String,String> attributesMap=new HashMap<String, String>();
				if(attributes!=null&&!"".equals(attributes)){
					attributesMap=(Map)JSON.parse(attributes);
				}
				StringBuffer str=new StringBuffer("<select");
				for (Map.Entry entry : attributesMap.entrySet()) {       
					String key = (String)entry.getKey( );
					str.append(" "+key+"=\""+attributesMap.get(key)+"\" ");
				}
				str.append(">");
				
				if(isShowPlease==null){
					isShowPlease=false;
				}
				
				if(isShowPlease){
					
					if(!StringUtils.isEmpty(oneShow)){
						str.append("<option value=''>"+oneShow+"</option>");
					}else{
						str.append("<option value=''>---请选择---</option>");
					}
				}
				
				if(has!=null&&!"".equals(has)){
					has+=",";
				}
				if(notHas!=null&&!"".equals(notHas)){
					notHas+=",";
				}
				
				for (DicNode dicNode : dicNodes) {
					String selected="";
					String code=dicNode.getStr("code");
					if(code.equals(value)){
						selected="selected";
					}
					if(has!=null&&!"".equals(has)){
						if(has.indexOf(code+",")>-1){
							str.append("<option "+selected+" value=\""+code+"\">"+dicNode.getStr("name")+"</option>");
						}
					}else if(notHas!=null&&!"".equals(notHas)){
						if(notHas.indexOf(code+",")==-1){
							str.append("<option "+selected+" value=\""+code+"\">"+dicNode.getStr("name")+"</option>");
						}
					}else{
						str.append("<option "+selected+" value=\""+code+"\">"+dicNode.getStr("name")+"</option>");
					}
				}
				
				str.append("</select>");
				
				out.print(str);
			}else{
				out.print("");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.EVAL_PAGE;
	}
}
